package sgd.tp2.client.net.udp;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

import sgd.tp2.api.logging.Log;
import sgd.tp2.api.net.CDRFormatType;
import sgd.tp2.client.net.Client;

public abstract class UDPClient extends Client {

	protected DatagramSocket socket = null;
	protected InetAddress serverIPAddress = null;
	protected int packetSize = 1024;

	public UDPClient(CDRFormatType cdrFormatType, String serverAddress, int serverPort) {
		super(cdrFormatType, serverAddress, serverPort);
	}

	public UDPClient(CDRFormatType cdrFormatType, String serverAddress, int serverPort,
			int packetSize) {
		super(cdrFormatType, serverAddress, serverPort);
		this.packetSize = packetSize;
	}

	@Override
	public void run() {
		while (running) {
		}
	}

	@Override
	public void startClient() throws UnknownHostException, IOException {
		socket = new DatagramSocket();
		serverIPAddress = InetAddress.getByName(serverAddress);
		Log.infoln("Iniciado cliente TCP:{0}:{1} a enviar o formato {2}.", serverAddress,
				serverPort, cdrFormatType);
	}

	@Override
	public void stopClient() {
		Log.insertString("\n");
		
		running = false;

		if (socket != null && !socket.isClosed())
			socket.close();

		Log.infoln("Servidor terminado com sucesso.");
	}
}
